
import { Controller, Inject, Get, Query, Param, Del, Post, Put, Body, createMiddleware, Files } from '@midwayjs/core';
import { ApiTags, ApiOperation, ApiResponse, ApiParam, ApiBody } from '@midwayjs/swagger';
import { Auth } from "../decorator/auth.decorator";
import { BusinessType, Log } from "../decorator/log.decorator";
import { StudentService } from '@service/student.service';
import { UpdateStudentDto, QueryStudentDto, createStudentUserDto } from '@dto/student.dto';
import { UploadMiddleware, UploadStreamFileInfo } from '@midwayjs/busboy';

@ApiTags('学生管理')
@Controller('system/student')
export class StudentController {
  @Inject()
  studentService: StudentService;
  // 创建
  @ApiOperation({ summary: '创建学生信息', description: '创建新的学生信息记录' })
  @ApiBody({ type: createStudentUserDto, description: '学生信息数据' })
  @ApiResponse({ status: 200, description: '创建成功' })
  @Auth('system:student:add')
  @Log({ title: '学生信息表-创建', businessType: BusinessType.ADD })
  @Post('/')
  async create(@Body() body: createStudentUserDto) {
    return await this.studentService.create(body);
  }
  // 获取列表
  @ApiOperation({ summary: '获取学生列表', description: '分页查询学生信息列表' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Auth('system:student:query')
  @Log({ title: '学生信息表-列表', businessType: BusinessType.LIST })
  @Get('/list')
  async list(@Query() query: QueryStudentDto) {
    return await this.studentService.list(query);
  }
  // 获取详情
  @ApiOperation({ summary: '获取学生详情', description: '根据学生ID获取学生详细信息' })
  @ApiParam({ name: 'studentId', description: '学生ID', type: 'string' })
  @ApiResponse({ status: 200, description: '查询成功' })
  @Auth('system:student:query')
  @Log({ title: '学生信息表-详情', businessType: BusinessType.DETAIL })
  @Get('/:studentId')
  async detail(@Param('studentId') studentId: string) {
    return await this.studentService.detail(studentId);
  }
  // 修改
  @ApiOperation({ summary: '更新学生信息', description: '更新学生信息记录' })
  @ApiBody({ type: UpdateStudentDto, description: '更新的学生信息数据' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Auth('system:student:edit')
  @Log({ title: '学生信息表-修改', businessType: BusinessType.EDIT })
  @Put('/')
  async update(@Body() body: UpdateStudentDto) {
    return await this.studentService.update(body);
  }
  @ApiOperation({ summary: '学生更新自己信息', description: '更新学生自己的信息记录' })
  @ApiBody({ type: UpdateStudentDto, description: '更新的学生信息数据' })
  @ApiResponse({ status: 200, description: '更新成功' })
  @Log({ title: '学生信息表-修改', businessType: BusinessType.EDIT })
  @Put('/info')
  async updateInfo(@Body() body: UpdateStudentDto) {
    return await this.studentService.update(body);
  }
  // 删除
  @ApiOperation({ summary: '删除学生信息', description: '根据学生ID删除学生信息，支持批量删除' })
  @ApiParam({ name: 'studentId', description: '学生ID，多个ID用逗号分隔', type: 'string' })
  @ApiResponse({ status: 200, description: '删除成功' })
  @Auth('system:student:remove')
  @Log({ title: '学生信息表-删除', businessType: BusinessType.REMOVE })
  @Del('/:studentId')
  async remove(@Param('studentId') studentId: string) {
    const studentIds = studentId.split(',').map((studentId) => +studentId);
    return this.studentService.remove(studentIds);
  }
  // 导出
  @ApiOperation({ summary: '导出学生信息', description: '根据查询条件导出学生信息到Excel文件' })
  @ApiBody({ type: QueryStudentDto, description: '查询条件' })
  @ApiResponse({ status: 200, description: '导出成功' })
  @Auth('system:student:export')
  @Log({ title: '学生信息表-导出', businessType: BusinessType.EXPORT })
  @Post('/export')
  async export(@Body() queryParams: QueryStudentDto) {
    return await this.studentService.export(queryParams);
  }
  //导出模板
  @ApiOperation({ summary: '导出学生信息模板', description: '导出学生信息的Excel模板文件' })
  @ApiResponse({ status: 200, description: '导出成功' })
  @Log({ title: '学生信息表-导出模板', businessType: BusinessType.EXPORT })
  @Post('/exportTemplate')
  async exportTemplate() {
    return await this.studentService.importTemplate();
  }
  // 导入模板数据
  @ApiOperation({ summary: '导入学生信息模板数据', description: '根据导入的Excel文件导入学生信息' })
  @ApiBody({ type: FormData, description: '导入的Excel文件' })
  @ApiResponse({ status: 200, description: '导入成功' })
  @Auth('system:student:import')
  @Log({ title: '学生信息表-导入', businessType: BusinessType.IMPORT })
  @Post('/importData', {
    middleware: [createMiddleware(UploadMiddleware, { mode: 'stream' })]
  })
  async importData(@Files() files: UploadStreamFileInfo,
    @Query('updateSupport') updateSupport: boolean) {
    return await this.studentService.importData({files, updateSupport});
  }
}
